package com.tool.payment.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.springframework.core.io.UrlResource;

public class ToolProperties extends Properties {
	
	private static final long serialVersionUID = 7889963496875417859L;

	private static final Logger logger = org.slf4j.LoggerFactory.getLogger(ToolProperties.class);

	public static String confPath= "file:/datum/data/conf/";
	
	public ToolProperties(String name) {
		FileInputStream fis = null;
		try {
			fis = this.getFileInputStream(name);
			if(fis == null) throw new RuntimeException("读取配置文件失败");
			//加载文件流的属性     
			this.load(fis);
		} catch (Exception e) {
			logger.info("读取配置文件错误");
		} finally {
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
				}
			}
		}
	}
	
	private FileInputStream getFileInputStream(String name) {
		String realPath = null;
		FileInputStream fis = null;
		try {
			UrlResource urlResource = new UrlResource(confPath + name);
			realPath = urlResource.getFile().getAbsolutePath();
			fis = new FileInputStream(new File(realPath));  
		} catch (Exception e1) {
			logger.info("读取配置文件错误1", realPath);
			try {
				realPath = ToolProperties.class.getClassLoader().getResource("").toURI().getPath();
				// 把文件读入文件输入流，存入内存中  
				fis = new FileInputStream(new File(realPath + name));  
			} catch (Exception e2) {
				logger.info("读取配置文件错误2", realPath);
			}
		}
		return fis;
	}

	public String getString(String key) {
		logger.debug(key);
		if(this.getProperty(key) != null) {
			return this.getProperty(key);
		}
		return "";
	}
	
	public String getString(String key,String defaultValue) {
		logger.debug(key);
		if(this.getProperty(key) != null) {
			return this.getProperty(key);
		}
		return defaultValue;
	}
	
	public Integer getInt(String key,Integer defaultValue) {
		logger.debug(key);
		if(this.getProperty(key) != null) {
				return Integer.valueOf(this.getProperty(key));
		}
		return defaultValue;
	}
}
